package controller_logger

import (
	"elflog/common/errs"
	"elflog/common/utils/str_util"
	"elflog/server/httpapi/ginutil"
	"github.com/gin-gonic/gin"
	"io/ioutil"
)

func (a *ControllerLogger) PushLog(c *gin.Context) {
	ctx := ginutil.MustGetAppCtx(c)

	var headerParam = &struct {
		Key       string `json:"key" header:"key" xml:"key" xml:"key"`
		Sign      string `json:"sign" header:"sign" xml:"sign" xml:"sign"`
		TimeStamp int64  `json:"time_stamp" header:"time_stamp",xml:"time_stamp" xml:"time_stamp"`
	}{}
	err2 := c.ShouldBindHeader(headerParam)
	if err2 != nil {
		a.log.Errorf("get head param err %v %v", str_util.ToJson(headerParam), err2)
		ginutil.ReturnWithErr(c, errs.NewParamError().WithError(err2))
		return
	}
	body, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		a.log.Errorf("read body error %v", err)
		ginutil.ReturnWithErr(c, err)
		return
	}
	a.log.Infof("on push log %v %v %v %v", headerParam.Key, headerParam.Sign, headerParam.TimeStamp, string(body))
	err = a.loggerService.PushLog(ctx, headerParam.TimeStamp, headerParam.Key, headerParam.Sign, string(body))
	if err != nil {
		ginutil.ReturnWithErr(c, err)
		return
	}
	ginutil.ReturnOk(c)
	return
}
